import java.util.Random;
class Creatarray{
   public static int[] elem;
   //初始化数组
   synchronized static public int[] array() throws InterruptedException {
       elem = new int[10000000];
       Thread thread = new Thread(){
           @Override
          public void run(){
               Random random = new Random();
               for (int i = 0; i < elem.length; i++) {
                   //给数组每个下标赋值
                   elem[i] = random.nextInt(100);
               }
          }
       };
       thread.start();
       thread.join();
       return elem;
   }
}
public class Test {
    static int evensum  = 0 ;
    static int sum  = 0 ;
    public static void main(String[] args) throws InterruptedException {
        //开始时间
        long starttime =System.currentTimeMillis();
        //初始化数组
        int[] array = Creatarray.array();
        /*for (int x: array) {
            System.out.print(x+" ");
        }
        System.out.println();*/
        Thread t1 = new Thread(){
            @Override
            public void run(){
                for (int i = 0; i < array.length; i++) {
                    if(i%2 == 0){
                        evensum += array[i];
                    }
                }
                System.out.println("偶数和为：" + evensum);
            }

        };
        //启动线程
        t1.start();
        Thread t2 = new Thread(){
            @Override
            public void run(){
                for (int i = 0; i < array.length; i++) {
                    if(i%2 != 0){
                        sum += array[i];
                    }
                }
                System.out.println("奇数和为：" + sum);
            }

        };
        t2.start();
        t1.join();
       t2.join();
       //求数组元素总和
       long addsum = sum + evensum;
       //结束时间
       long endtime = System.currentTimeMillis();
        System.out.println("数组元素总和为：" + addsum);
        System.out.println("总运行时间为：" + (endtime - starttime));
    }
}
